package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.park_management.entity.MarketingCampaigns_wjs;
import com.example.park_management.mapper.MarketingCampaignsMapper_wjs;
import com.example.park_management.service.MarketingCampaignsService_wjs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;

@RestController
@RequestMapping("/MarketingManagementSystem")
public class MarketingCampaignsController_wjs {
    @Autowired
    private MarketingCampaignsService_wjs service_wjs;

    @GetMapping
    public List<MarketingCampaigns_wjs> index() {
        return service_wjs.list();
    }

//    @GetMapping("/findMcByUserId")
//    public List<MarketingCampaigns_wjs> findMcByUserId(Integer userId) {
//        return service_wjs.findMcByUserId(userId);
//    }

    @PostMapping
    public boolean savaMC_wjs(@RequestBody MarketingCampaigns_wjs marketingCampaignsWjs) {
        return service_wjs.savamcwjs(marketingCampaignsWjs);
    }

    @DeleteMapping("/{id}")
    public boolean delete_MC(@PathVariable Integer id) {
       return service_wjs.removeById(id);
    }

    //分页查询
    //接口 /MarketingManagementSystem/page
//    @GetMapping("/page")
//    public Map<String,Object> findPage(@RequestParam Integer pageNum,
//                                       @RequestParam Integer pageSize) {
//         pageNum = (pageNum - 1) * pageSize;
//        Integer total = mapper_wjs.seleteTitle();
//        List<MarketingCampaigns_wjs> date = mapper_wjs.selectPage(pageNum,pageSize);
//        Map<String, Object> res = new HashMap<>();
//        res.put("data",date);
//        res.put("total",total);
//        return res;
//    }

    //mybatis-plus版
    @GetMapping("/page")
    public IPage<MarketingCampaigns_wjs> findPage(@RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize,
                                                  @RequestParam(defaultValue = "") String campaignName,
                                                  @RequestParam(defaultValue = "") String campaignType) {
        System.out.println(campaignName);
        IPage<MarketingCampaigns_wjs> page = new Page<>(pageNum, pageSize);
        QueryWrapper<MarketingCampaigns_wjs> queryWrapper = new QueryWrapper<>();
        if (!"".equals(campaignName)) {
            queryWrapper.like("campaignName", campaignName);
        }
        if (!"".equals(campaignType)) {
            queryWrapper.like("campaignType", campaignType);
        }
//        queryWrapper.or().like("")
        queryWrapper.orderByDesc("campaignID");
        return service_wjs.page(page,queryWrapper);
    }
}
